package cn.vc.content.service.video;

import cn.vc.content.entity.video.VideoLike;
import cn.vc.content.mapper.video.VideoLikeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class VideoLikeService {
    private final VideoLikeMapper videoLikeMapper;

    public VideoLikeService(VideoLikeMapper videoLikeMapper) {
        this.videoLikeMapper = videoLikeMapper;
    }


    public boolean like(Long id, Long userId) {
        VideoLike videoLike = new VideoLike();
        videoLike.setVideoId(id);
        videoLike.setUserId(userId);
        try {
            videoLikeMapper.insert(videoLike);
            return true;
        } catch (Exception e) {
            log.debug("已经点过赞了："+userId+" -> "+id);
            this.noLike(id, userId);
            return false;
        }
    }


    public int noLike(Long id, Long userId) {
        LambdaQueryWrapper<VideoLike> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoLike::getVideoId, id).eq(VideoLike::getUserId, userId);
        VideoLike videoLike = videoLikeMapper.selectOne(lqw);
        if (videoLike == null) return -1;
        videoLikeMapper.deleteById(videoLike.getId());
        return 1;
    }
}
